import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from 'vue-router'

import { useAccountStore } from '@/stores/account'
import { storeToRefs } from 'pinia'
export let routes = [
  {
    path: '/',
    redirect: '/private/db-management',
  },
  {
    path: '/private',
    name: 'private',
    component: () => import('@/layout/private-layout/private-layout.vue'),
    children: [],
  },
  {
    path: '/sign',
    name: 'sign',
    component: () => import('@/layout/sign-layout/sign-layout.vue'),
    children: [
      {
        path: 'login',
        name: 'login',
        component: () => import('@/views/login/login.vue'),
        beforeEnter(to, from) {
          let accountStore = useAccountStore()
          let account = storeToRefs(accountStore)
          /**
           * 登录状态禁止到登录页
           */
          if (account.token.value) {
            return false
          }
        },
      },
    ],
  },
  {
    path: '/:catchAll(.*)',
    component: () => import('@/views/error-page/error-page.vue'),
  },
]

if (import.meta.env.DEV) {
  routes.unshift({
    path: '/example-components',
    name: 'example-components',
    component: () =>
      import('@/views/example/example-components/example-components.vue'),
  })
  routes.unshift({
    path: '/example-api',
    name: 'example-api',
    component: () => import('@/views/example/example-api.vue'),
  })
  routes.unshift({
    path: '/example-excel',
    name: 'example-excel',
    component: () => import('@/views/example/example-excel.vue'),
  })
  routes.unshift({
    path: '/example-docx',
    name: 'example-docx',
    component: () => import('@/views/example/example-docx.vue'),
  })
}

let router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
})

let routesPrivate = [
  {
    path: 'db-management',
    name: 'db-management',
    meta: { title: '数据库管理' },
    component: () => import('@/views/db-management/db-management.vue'),
  },
  {
    path: 'db-management-preview/:id/:fileid',
    name: 'db-management-preview',
    meta: { title: '数据库管理预览' },
    component: () =>
      import('@/views/db-management-preview/db-management-preview.vue'),
  },
  {
    path: 'user-management',
    name: 'user-management',
    meta: { title: '人员管理' },
    component: () => import('@/views/user-management/user-management.vue'),
  },
  {
    path: 'rights-management',
    name: 'rights-management',
    meta: { title: '权限管理' },
    component: () => import('@/views/rights-management/rights-management.vue'),
  },
  {
    path: 'pw-management',
    name: 'pw-management',
    meta: { title: '密码管理' },
    component: () => import('@/views/pw-management/pw-management.vue'),
  },
  {
    path: 'catalog-management',
    name: 'catalog-management',
    meta: { title: '层级定义' },
    component: () =>
      import('@/views/catalog-management/catalog-management.vue'),
  },
  {
    path: 'audit-management',
    name: 'audit-management',
    meta: { title: '审计' },
    component: () => import('@/views/audit-management/audit-management.vue'),
  },
]

export function useRoutesPrivate() {
  return {
    permission(options) {
      if (options.adminType === 'normal') {
        routesPrivate.forEach(route => {
          router.addRoute('private', route)
        })
      }
      if (options.adminType === 'security') {
      }
      if (options.adminType === 'system') {
      }
      router.replace(router.currentRoute.value.fullPath)
    },
  }
}
console.log(router)
export default router
